import axios from 'axios'
import qs from 'qs'
import Vue from 'vue'
import _store from './index'
import _router from '../router/index'

//axios.defaults.headers.common['Token'] = _this.getters.token; 
axios.defaults.baseURL=process.env.BASE_URL //设置域名
axios.defaults.withCredentials=false;

function  get({url, data,msg}) {
  console.log(process.env.BASE_URL+url)
    return new Promise((resolve, reject) => {
      axios.get(url, {
          params: data
      },
      ).then(res => {
           if(msg){
              if(msg.show)
               ReturnMessag(msg);
           }
           resolve(res.data)
      }).catch(err => {
          if(msg){
              if(msg.show)
               ReturnMessag(msg,4);
           }
            reject(err)
      })
    })
  }

 function  post({url, data,msg}) {
  console.log(process.env.BASE_URL+url)
    return new Promise((resolve, reject) => {
      axios.post(url, JSON.stringify (data), {
           headers: {
             //'Content-Type': 'application/x-www-form-urlencoded', 
             'Content-Type': 'application/json',
           }
        }
      ).then(res => {
          if(msg){
              if(msg.show)
               ReturnMessag(msg);
           }
        resolve(res.data)
      }).catch(err => {
          if(msg){
              if(msg.show)
               ReturnMessag(msg,4);
           }
        reject(err)
      })
    })
  }


     //登录
     function Login(){
      _router.push({path:'/User/Login'})
    }
    //退出登录
    function LogOut(){
      _store.dispatch('setLogOut');
    }


  Vue.prototype.get=get;
  Vue.prototype.post=post;
  Vue.prototype.alert=alert;
  Vue.prototype.Login=Login;
  Vue.prototype.LogOut=LogOut;

//弹出提示窗口

function ReturnMessag(msg={show,top,left},option){
 
    // "<div class='alert alert-warning'><a href='#' class='close' data-dismiss='alert'>&times;</a><strong>警告！</strong>您的网络连接有问题。</div>"
    var result="";
    var style="";
    if(msg.top && msg.left){
        style='style="width: auto;min-width: 18%;position: fixed;top: '+msg.top+'%;left: '+msg.left+'%;transform: translate(-50%,-50%);z-index: 9999;"';
    }else{
        style='style="width: auto;min-width: 18%;position: fixed;top: 90%;left: 90%;transform: translate(-50%,-50%);z-index: 999;"';
    }
   
    switch(option){
        case 1:
        result='<div class="alert alert-success" role="alert"  '+style+' ><a href="#" class="close" data-dismiss="alert">&times;</a>操作成功</div>';
        break;
        case 2:
        result='<div class="alert alert-info" role="alert" '+style+'><a href="#" class="close" data-dismiss="alert">&times;</a>信息提示</div>';
        break;
        case 3:
        result='<div class="alert alert-warning" role="alert" '+style+'><a href="#" class="close" data-dismiss="alert">&times;</a>警告</div>';
        break;
        case 4:
        result='<div class="alert alert-danger" role="alert" '+style+'><a href="#" class="close" data-dismiss="alert">&times;</a>错误</div> ';
        break;
        default:
        result='<div class="alert alert-success" role="alert" '+style+'><a href="#" class="close" data-dismiss="alert">&times;</a>操作成功</div>';
        break
    }
    EmptyReturnMessag()
    _store.dispatch('setMsgContent',result);
}

//N秒后清除提示窗口
var objtime
function EmptyReturnMessag(){
  window.clearTimeout(objtime);   
  objtime =  window.setTimeout(()=>{
    _store.dispatch('setMsgContent',"");
  },2500); 
}

//alert
function alert(value){
  var result="";
  var style="";
  style='style="width: auto;min-width: 10%;position: fixed;top: 55%;left: 49%;transform: translate(-50%,-50%);z-index: 999;color: #fff;background-color: #000;border-color: #c3e6cb;border: none;opacity: 0.6;border-radius: -0rem;text-align: center;';
  result='<div class="alert " role="alert"  '+style+' ><a href="#" class="close" data-dismiss="alert"></a>'+value+'</div>';
  EmptyReturnMessag();
  _store.dispatch('setMsgContent',result);
}

